package com.boss.salary.controller;

import com.boss.api.dto.ApiResultDTO;
import com.boss.common.base.BasicinfoBaseController;
import com.boss.salary.entity.SalPostStandardSalary;
import com.boss.salary.service.ISalPostStandardSalaryService;
import com.boss.sys.ca.user.dto.UserDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/salPostStandardSalaryController", produces = MediaType.APPLICATION_JSON)
public class SalPostStandardSalaryController extends BasicinfoBaseController {

    /**
     * 引入日志框架常量
     */
    private final static Logger logger = LoggerFactory.getLogger(SalPostStandardSalaryController.class);

    @Autowired
    private ISalPostStandardSalaryService iSalPostStandardSalaryService;

    /**
     * 查询全部
     *
     * @return
     */
    @RequestMapping("/querySalPostStandardSalaryAll")
    public ApiResultDTO querySalPostStandardSalaryAll(@RequestHeader("tokenid") String tokenid,
                                                      @RequestParam(value = "type", required = false) String type,
                                                      @RequestParam(value = "salaryId", required = false) String salaryId) {
        try {
            UserDTO userDTO = getUser(tokenid);
            Map<String, Object> queryParam = new HashMap<>();
            queryParam.put("type", type);
            queryParam.put("salaryId", salaryId);
            queryParam.put("mofDivCode", userDTO.getProvince());
            List<Map<String, Object>> pageDTO = iSalPostStandardSalaryService.querySalPostStandardSalaryAll(queryParam);
            return ApiResultDTO.success(pageDTO, null);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询数据异常");
        }
    }

    /**
     * 查询标准工资数
     *
     * @return
     */
    @GetMapping("/getSalByGradeLevelId")
    public ApiResultDTO getSalByGradeLevelId(@RequestHeader("tokenid") String tokenid,
                                                      @RequestParam(value = "gradeLevelId") String gradeLevelId) {
        try {
            UserDTO userDTO = getUser(tokenid);
            Map<String, Object> queryParam = new HashMap<>();
            queryParam.put("gradeLevelId", gradeLevelId);
            queryParam.put("mofDivCode", userDTO.getProvince());
            List<Map<String, Object>> pageDTO = iSalPostStandardSalaryService.getSalByGradeLevelId(queryParam);
            if(!CollectionUtils.isEmpty(pageDTO)){
                return ApiResultDTO.success(pageDTO, null);
            }
            queryParam.put("salaryId",-1);
            queryParam.put("salaryMoney",0);
            return ApiResultDTO.success(queryParam, null);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询数据异常");
        }
    }

    /**
     * 根据code查询
     * @param salaryId
     * @param gradeLevelId
     * @param personTypeId
     * @param postId
     * @param type
     * @return
     */
    @GetMapping("/checkRepeat")
    public ApiResultDTO checkRepeat(@RequestParam("salaryId") String salaryId, @RequestParam("personTypeId") String personTypeId,
                                        @RequestParam("postId") String postId, @RequestParam("gradeLevelId") String gradeLevelId,
                                        @RequestParam("type") String type){
        try {
            Map<String, String> queryParams = new HashMap<>(5);
            queryParams.put("salaryId", salaryId);
            queryParams.put("personTypeId", personTypeId);
            queryParams.put("postId", postId);
            queryParams.put("gradeLevelId", gradeLevelId);
            queryParams.put("type", type);
            int count = iSalPostStandardSalaryService.checkRepeat(queryParams);
            return ApiResultDTO.success(count, null);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询数据异常");
        }
    }

    /**
     * 新增
     *
     * @param salPostStandardSalary
     * @return
     */
    @PostMapping("/saveSalPostStandardSalary")
    public ApiResultDTO saveSalPostStandardSalary(@RequestHeader("tokenid") String tokenid,
                                                  @RequestBody SalPostStandardSalary salPostStandardSalary) {
        try {
            String province = getUser(tokenid).getProvince();
            salPostStandardSalary.setMofDivCode(province);
            iSalPostStandardSalaryService.insertSalPostStandardSalary(salPostStandardSalary);
            return ApiResultDTO.success(null, "保存成功");
        } catch (Exception e) {
            logger.error("新增 标准工资发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "新增 标准工资发生异常");
        }
    }

    /**
     * 修改
     *
     * @param salPostStandardSalary
     * @return
     */
    @PostMapping("/updateSalPostStandardSalary")
    public ApiResultDTO updateSalPostStandardSalary(@RequestBody SalPostStandardSalary salPostStandardSalary) {
        try {
            iSalPostStandardSalaryService.updateSalPostStandardSalary(salPostStandardSalary);
            return ApiResultDTO.success(null, "修改成功");
        } catch (Exception e) {
            logger.error("修改 标准工资表发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "修改 标准工资发生异常");
        }
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deleteSalPostStandardSalary/{id}")
    public ApiResultDTO deleteSalPostStandardSalary(@PathVariable("id") String id) {
        try {
            iSalPostStandardSalaryService.deleteSalPostStandardSalary(id);
            return ApiResultDTO.success(null, "删除成功");
        } catch (Exception e) {
            logger.error("删除 标准工资发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "删除 标准工资发生异常");
        }
    }

}
